package com.angel.controller;

import com.angel.bean.*;
import com.angel.service.*;
import com.angel.utils.OrderBin;
import com.angel.utils.UserUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户中心
 */
@Controller
@RequestMapping("/c")
public class UserCententController {
    @Autowired
    private VipCardService vipCardService;

    @Autowired
    private IllCaseService illCaseService;

    @Autowired
    private PrescriptionService prescriptionService;

    @Autowired
    private VipInfoService vipInfoService;

    private OutWebResourceUrlService outWebResourceUrlService;

    /**
     * 个人中心界面数据,查看用户是否添加宝宝了
     *
     * @return 返回可用查询与否
     */
    @RequestMapping("/centre")
    public String toString(Model model) {
        UserInfo userInfo = UserUtils.getPrincipal().getUserInfo();
        //用户名
        model.addAttribute("userName", userInfo.getUserName());
        model.addAttribute("photo", userInfo.getUserPhoto());
        System.out.println(UserUtils.getPrincipal().getUserInfo().getUserName() + "--");

        //查询会员信息
        VipInfo vipInfo = new VipInfo();
        vipInfo.setUserId(UserUtils.getPrincipal().getUserInfo().getId());
        VipInfo result = vipInfoService.findOne(vipInfo);
        VipCard vipCard = new VipCard();
        //返回VIP信息结果集
        VipCard result_vipvad = null;
        //判断是学员还是会员
        if ("1".equals(userInfo.getReserve1())) {
            //学员，做相关的处理
            model.addAttribute("vipTrueFalse", true);
            model.addAttribute("vipName", "欢迎学员");
            //余额
            model.addAttribute("balancebalance", 0);
        } else {
            //下面是会员的逻辑处理
            if (result == null) {
                model.addAttribute("vipTrueFalse", false);
                //vip 等级名称
                model.addAttribute("vipName", "未激活");
                //余额
                model.addAttribute("balancebalance", "0");
            } else {
                vipCard.setVipId(result.getVipId());
                result_vipvad = vipCardService.findOne(vipCard);
                model.addAttribute("vipTrueFalse", true);
                //vip 等级名称
                model.addAttribute("vipName", result_vipvad.getVipName());
                //余额
                model.addAttribute("balancebalance", result.getBalance());
            }
        }
        return "user_content";
    }

    /**
     * 查询订单
     *
     * @param model
     * @return 查询成功与否true成功，false没有宝宝不能查询，null数据异常
     */
    @RequestMapping("/order1")
    public String order(Model model) {
        try {
            System.out.println("病例表用户id" + UserUtils.getPrincipal().getUserInfo().getId());
            //病历表
            IllCase illCase = new IllCase();
            //用户id
            illCase.setUserId(UserUtils.getPrincipal().getUserInfo().getId());
            //用户id来查
            List<IllCase> illCase_result = illCaseService.findIllCase(illCase);
            //illCase_result.getIllCaseId();//获取病历id
            List<OrderBin> orderBinList = new ArrayList<>();
            for (int i = 0; i < illCase_result.size(); i++) {
                OrderBin orderBin = new OrderBin();
                //处方表
                Prescription prescription = new Prescription();
                //用于查询
                prescription.setIllCaseId(illCase_result.get(i).getIllCaseId());
                //开单时间
                orderBin.setData(illCase_result.get(i).getVisitDate().toString());
                //处方编号
                orderBin.setNo(illCase_result.get(i).getIllCaseNo());
                //用病例id来关联查询价格
                if (illCase_result.get(i).getIllCaseState() == 6){
                    orderBin.setPrice(prescriptionService.findon(prescription).getReserve1());
                }else {
                    orderBin.setPrice("0");
                }
                orderBinList.add(orderBin);
            }
            PageHelper.startPage(1, 100);
            PageInfo<OrderBin> page = new PageInfo<>(orderBinList);
            if (illCase_result!=null){
                //有订单
                model.addAttribute("emp", true);
                if (page!=null){
                    model.addAttribute("pageInfo", page);
                }
            }else {
                //没有订单
                model.addAttribute("emp", false);
            }
        } catch (Exception e) {

        }
        return "user_orde";
    }


    @RequestMapping("/order")
    public String getOrder(Model model,@RequestParam(required = false) String pageNum) {
        try {
            Integer userId= UserUtils.getPrincipal().getUserInfo().getId();

            int pageNo = 1;
            try {
                pageNo = Integer.valueOf(pageNum);
            } catch (Exception e) {
                pageNo =1;
            }
            PageHelper.startPage(pageNo, 10);
            List<Prescription> list = prescriptionService.getPrescriptionByUserId(userId,6);


            if (list!=null){
                //有订单
                PageInfo<Prescription> page = new PageInfo<>(list);
                model.addAttribute("emp", true);
                if (page!=null){
                    model.addAttribute("pageInfo", page);
                }
            }else {
                //没有订单
                model.addAttribute("emp", false);
            }
        } catch (Exception e) {

        }
        return "user_orde";
    }


    /**
     * 删除订单
     * @param id
     * @return
     */
    @RequestMapping("/delet")
    @ResponseBody
    public Object deletId(Integer id){
        Map<String,String> map=new HashMap<>();
        return map;
    }
}
